package com.lilicould.controller;

import com.lilicould.entity.House;
import com.lilicould.service.HouseService;
import com.lilicould.service.impl.HouseServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/house/add")
public class HouseAddController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String employeeId = req.getParameter("employeeId");
        String buildingId = req.getParameter("buildingId");
        String apartmentNo = req.getParameter("apartmentNo");
        Integer bedroomCount =Integer.parseInt(req.getParameter("bedroomCount"));
        Integer bathroomCount =Integer.parseInt(req.getParameter("bathroomCount"));
        String address = req.getParameter("address");

        House house = new House(buildingId, apartmentNo, bedroomCount, bathroomCount, address, employeeId, null, null);

        HouseService houseService = new HouseServiceImpl();
        boolean isYours = houseService.isYours(employeeId, buildingId);
        boolean result; // 默认为false
        if (!isYours) {
            req.setAttribute("error", "此栋楼归其他经理管理");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
            return;
        }else {
            result = houseService.insertHouse(house);
        }

        if (result) {
            req.getRequestDispatcher("/employee/personalInfo").forward(req, resp);
        }else {
            req.setAttribute("error", "插入失败");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }

        // 打印参数
//        System.out.println("employeeId:"+employeeId + "\n"
//                +"buildingId:"+buildingId + "\n"
//                +"apartmentNo:"+apartmentNo + "\n"
//                +"bedroomCount:"+bedroomCount + "\n"
//                +"bathroomCount:"+bathroomCount + "\n"
//                +"address:"+address + "\n"
//        );
    }
}
